package org.ly.sys.module;

import org.ly.sys.bean.User;
import org.ly.utils.MD5;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.service.EntityService;

import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@At("/User")
@IocBean(fields={"dao"})
public class UserModule extends EntityService<User>{

    private static final Log log = Logs.get();
    
    @At
    @Ok("jsp:views.sys.addUser")
    public void add() {
        
    }
	
	@At
	@Ok("jsp:views.sys.listUser")
	public Object list(@Param("page") int page ,@Param("rows") int rows){
		if (rows < 1)
			rows = 10;
		if(page < 1)
			page = 1;
		Pager pager = dao().createPager(page, rows);
		List<User> list = dao().query(User.class, null, pager);
		Map<String, Object> map = new HashMap<String, Object>(0);
		if (pager != null) {
			pager.setRecordCount(dao().count(User.class));
			map.put("pager", pager);
		}
		map.put("list", list);
		return map;
	}
	
	@At
	@Ok("redirect:/User/list")
	public void save(@Param("..") User obj){
		try{
			obj.setPassword(MD5.getMd5(obj.getPassword()));
			dao().insert(obj);
		}catch (Throwable e) {
			if (log.isDebugEnabled())
				log.debug("E!!",e);
		}
	}
	
	@At
	@Ok("redirect:/User/list")
	public void delete(@Param("userId") Integer userid){
		try{
			dao().delete(dao().fetch(User.class, userid));
		}catch (Throwable e) {
			if (log.isDebugEnabled())
				log.debug("E!!",e);
		}
	}
	
	@At
	@Ok("redirect:/User/list")
	public void update(@Param("..") User obj){
		try{
			dao().update(obj);
		}catch (Throwable e) {
			if (log.isDebugEnabled())
				log.debug("E!!",e);
		}
	}
	
	@At
	@Ok("redirect:/User/list")
	public void reset(@Param("userId") Integer userid) {
	    User user = dao().fetch(User.class, userid);
	    //重置密码为123456
	    user.setPassword(MD5.getMd5("123456"));
	    dao().update(user);
	}
	
	@At
	@Ok("jsp:views.sys.editPassword")
	public void goEditPassword() {
	    
	}
	
	@At
	@Ok("jsp:views.sys.editPassword")
	@Fail("jsp:views.sys.editPassword")
	public Object editPassword(@Param("oldpassword") String oldpassword, @Param("password") String password, HttpServletRequest request) {
	    String reString = "";
	    User loginUser = (User) Mvcs.getHttpSession().getAttribute("loginuser");
	    if(!MD5.getMd5(oldpassword).equals(loginUser.getPassword())) {
	        throw new RuntimeException("旧密码错误!");
	    }else if(MD5.getMd5(password).equals(loginUser.getPassword())){
	            throw new RuntimeException("新密码与旧密码相同!");
	    }else{
	        loginUser.setPassword(MD5.getMd5(password));
	        dao().update(loginUser);
	        reString = Mvcs.getMessage(request, "editpasswordok");
	    }
	    return reString;
	}
}